package com.yanwq.sayhi.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：yanweiqiang。
 * 日期：2016/6/16 0016。
 */
public class SelectDialog<T> extends BaseDialog {
    @BindView(R.id.select_list)
    ListView mListView;

    List<Item<T>> mItems;

    private OnSelectedListener<T> onSelectedListener;

    public void setOnSelectedListener(OnSelectedListener<T> onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public SelectDialog(Context context, List<Item<T>> items) {
        super(context);
        mItems = items;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select);
        ButterKnife.bind(this);

        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return mItems.size();
            }

            @Override
            public Object getItem(int position) {
                return mItems.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;

                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_select, parent, false);
                    holder.textView = (TextView) convertView.findViewById(R.id.select_text);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                final Item<T> item = mItems.get(position);

                holder.textView.setText(item.name);

                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onSelectedListener.onSelected(item.name, item.data);
                        cancel();
                    }
                });

                return convertView;
            }

            class ViewHolder {
                TextView textView;
            }
        };

        mListView.setAdapter(adapter);
    }

    public interface OnSelectedListener<T> {
        void onSelected(String item, T t);
    }

    public static class Item<T> {
        String name;
        T data;

        public Item(String name, T data) {
            this.name = name;
            this.data = data;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }
}